package com.nakamachizu.controller.cron;

import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;
import java.util.logging.Logger;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;
import org.slim3.datastore.Datastore;

import com.nakamachizu.meta.MapDataMeta;
import com.nakamachizu.model.MapData;
import com.nakamachizu.service.MapDataService;

public class CleanController extends Controller {

    private static final Logger log = Logger.getLogger(CleanController.class.getName());
    
    private MapDataMeta meta = MapDataMeta.get();
    
    @Override
    public Navigation run() {
        Calendar now = Calendar.getInstance(TimeZone.getTimeZone("Asia/Tokyo"));
        List<MapData>list = Datastore.query(meta).filter(meta.expire.lessThan(now.getTime())).asList();
        if(list != null){
            log.info("削除対象地図" + Integer.toString(list.size()) + "件です。");
            MapDataService mds = new MapDataService();
            for(MapData data : list){
                mds.deleteMapData(data);
            }
            log.info("削除対象地図" + Integer.toString(list.size()) + "件の削除が完了しました。");
        }
        return null;
    }
}
